<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$gold_offer = getPostValue( 'gold_offer' );
$iron_offer = getPostValue( 'iron_offer' );
$coal_offer = getPostValue( 'coal_offer' );
$food_offer = getPostValue( 'food_offer' );
$gold_request = getPostValue( 'gold_request' );
$iron_request = getPostValue( 'iron_request' );
$coal_request = getPostValue( 'coal_request' );
$food_request = getPostValue( 'food_request' );
$multiplier = getPostValue( 'multiplier' );
$type = getPostValue( "type" );
		
if( isset( $_POST[ 'calculate' ] ) ) {
	$calc = true;
}
else {
	$calc = false;
}

if( $gold_offer == "" ) {
	$gold_offer = "0";
}
if( $iron_offer == "" ) {
	$iron_offer = "0";
}
if( $coal_offer == "" ) {
	$coal_offer = "0";
}
if( $food_offer == "" ) {
	$food_offer = "0";
}
if( $gold_request == "" ) {
	$gold_request = "0";
}
if( $iron_request == "" ) {
	$iron_request = "0";
}
if( $coal_request == "" ) {
	$coal_request = "0";
}
if( $food_request == "" ) {
	$food_request = "0";
}
$flag = false;

if( $multiplier < 1 ) {
	$multiplier = 1;
	$status = translate( "If you want to create less then 1 trade offer, you shouldn't create a trade offer!" );
	$flag = true;
}
elseif( $multiplier > 3 && $premium < 1 ) {
	$multiplier = 3;
	$status = translate( "You can only create a maximum of 3 trades offers at once without premium turns." );
	$flag = true;
}
elseif( $multiplier > 9 && $premium > 0 ) {
	$multiplier = 9;
	$status = translate( "You can only create a maximum of 9 trades offers at once." );
	$flag = true;
}

if( !is_numeric( $gold_offer ) || !is_numeric( $iron_offer ) || !is_numeric( $coal_offer ) || !is_numeric( $food_offer ) || 
	!is_numeric( $gold_request ) || !is_numeric( $iron_request ) || !is_numeric( $coal_request ) || !is_numeric( $food_request ) ) {
	$flag = true;
	$status = translate( "Make sure values are set properly." );
}

if( $gold_offer > 0 && $gold_request > 0 ) {
	$flag = true;
	$status = translate( "You can't trade gold for gold!" );
	if( $gold_offer > $gold_request ) {
		$gold_offer -= $gold_request;
		$gold_request = 0;
	}
	else {
		$gold_request -= $gold_offer;
		$gold_offer = 0;
	}
}

if( $iron_offer > 0 && $iron_request > 0 ) {
	$flag = true;
	$status = translate( "You can't trade iron for iron!" );
	if( $iron_offer > $iron_request ) {
		$iron_offer -= $iron_request;
		$gold_request = 0;
	}
	else {
		$iron_request -= $iron_offer;
		$iron_offer = 0;
	}
}

if( $coal_offer > 0 && $coal_request > 0 ) {
	$flag = true;
	$status = translate( "You can't trade coal for coal!" );
	if( $coal_offer > $coal_request ) {
		$coal_offer -= $coal_request;
		$coal_request = 0;
	}
	else {
		$coal_request -= $coal_offer;
		$coal_offer = 0;
	}
}

if( $food_offer > 0 && $food_request > 0 ) {
	$flag = true;
	$status = translate( "You can't trade food for food!" );
	if( $food_offer > $food_request ) {
		$food_offer -= $food_request;
		$food_request = 0;
	}
	else {
		$food_request -= $food_offer;
		$food_offer = 0;
	}
}

$result2 = mysql_query( "SELECT coal_gold, iron_gold, food_gold, date, turn " .
		"FROM game" ) or die ( "Query failed: " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
//$gold_iron = 1 / $row2[ 'iron_gold' ];
//$gold_coal = 1 / $row2[ 'coal_gold' ];
//$gold_food = 1 / $row2[ 'food_gold' ];
//$iron_coal = $row2[ 'iron_gold' ] / $row2[ 'coal_gold' ];
//$coal_iron = 1 / $iron_coal;
//$food_coal = $row2[ 'food_gold' ] / $row2[ 'coal_gold' ];
//$coal_food = 1 / $food_coal;
//$iron_food = $row2[ 'iron_gold' ] / $row2[ 'food_gold' ];
//$food_iron = 1 / $iron_food;

$date = $row2[ 'date' ];
$turn = $row2[ 'turn' ];

$value_offered = $gold_offer + $row2[ 'iron_gold' ] * $iron_offer + $row2[ 'coal_gold' ] * $coal_offer + $row2[ 'food_gold' ] * $food_offer;
$value_requested = $gold_request + $row2[ 'iron_gold' ] * $iron_request + $row2[ 'coal_gold' ] * $coal_request + $row2[ 'food_gold' ] * $food_request;

$value_difference = $value_offered * 0.5;
if( !$flag && $value_offered + $value_difference < $value_requested ) {
	$status = translate( "Your trade offer has been denied, you requested too much." );
	if( $calc ) {
		$status = translate( "Your trade offer will be denied, you request too much." );
	}
	$flag = true;
}

if( !$flag && $value_offered - $value_difference > $value_requested ) {
	$status = translate( "Your trade offer has been denied, you requested too little." );
	if( $calc ) {
		$status = translate( "Your trade offer will be denied, you request too little." );
	}
	$flag = true;
}

if( !$flag && ( $value_offered < 1 || $value_requested < 1 ) ) {
	$status = translate( "You can't trade nothing!" );
	if( $calc ) {
		$status = translate( "Your trade will be denied, you can't trade nothing." );
	}
	$flag = true;
}

if( $calc && !$flag ) {
	$flag = true;
	$status = translate( "This is a valid trade." );
	if( $value_offered != 0 && $value_requested != 0 ) {
		$offer_quality = $value_offered / $value_requested;
		
		if( $offer_quality < 0.90 ) {
			$color = "#EE1111";
			$rating = "very expensive";
		}
		else if( $offer_quality < 0.98 ) {
			$color = "#AA2222";
			$rating = "expensive";
		}
		else if( $offer_quality > 1.10 ) {
			$color = "#33AA33";
			$rating = "very cheap";
		}
		else if( $offer_quality > 1.02 ) {
			$color = "#117711";
			$rating = "cheap";
		}
		else {
			$color = "#2222CC";
			$rating = "fair";
		}
		
		$_SESSION[ 'trade_offer_rating' ] = "<strong style='color: $color'>" . translate( $rating ) . "</strong>";
	}
}

if( $flag ) {
	if( isset( $status ) ) {
		$_SESSION[ 'create_trade_status' ] = $status;
	}
	
	$_SESSION[ 'gold_offer' ] = $gold_offer;
	$_SESSION[ 'iron_offer' ] = $iron_offer;
	$_SESSION[ 'coal_offer' ] = $coal_offer;
	$_SESSION[ 'food_offer' ] = $food_offer;
	$_SESSION[ 'gold_request' ] = $gold_request;
	$_SESSION[ 'iron_request' ] = $iron_request;
	$_SESSION[ 'coal_request' ] = $coal_request;
	$_SESSION[ 'food_request' ] = $food_request;
	$_SESSION[ 'value_offered' ] = $value_offered;
	$_SESSION[ 'value_requested' ] = $value_requested;
	$_SESSION[ 'multiplier' ] = $multiplier;
	
	$_SESSION[ 'trade_access' ] = getPostValue( "type" );
	$_SESSION[ 'trade_length' ] = getPostValue( "length" );
	$_SESSION[ 'trade_as' ] = getPostValue( "from" );
	$_SESSION[ 'trade_access_name' ] = getPostValue( "private_name" );

	header( "Location: create_trade.php" );
}
else {
	$from = getPostValue( "from" );
	if( $from == 1 ) {
		$result = mysql_query( "SELECT * " .
				"FROM faction " .
				"WHERE leader = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( !isset( $row[ 'faction_id' ] ) ) {
			$status = translate( "You are not a faction leader!" );
		}
		else {
			$tmp_faction_id2 = $row[ 'faction_id' ];
		}
	}
	else {
		$result = mysql_query( "SELECT * " .
				"FROM ruler " .
				"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	}
	
	if( !isset( $status ) ) {
		$has_gold = $row[ 'gold' ]; 
		$has_iron = $row[ 'iron' ]; 
		$has_coal = $row[ 'coal' ]; 
		$has_food = $row[ 'food' ]; 
		
		if( $has_gold < $gold_offer * $multiplier ) {
			$status = translate( "You don't have enough gold." );
		}
		if( $has_iron < $iron_offer * $multiplier ) {
			$status = translate( "You don't have enough iron." );
		}
		if( $has_coal < $coal_offer * $multiplier ) {
			$status = translate( "You don't have enough coal." );
		}
		if( $has_food < $coal_food * $multiplier ) {
			$status = translate( "You don't have enough food." );
		}
		
		$length = getPostValue( "length" );
		if( $length == 0 ) {
			$time = 24;
			$cost = 0;
		}
		else if( $length == 1 ) {
			if( $has_gold < $gold_offer * $multiplier + 10 * $multiplier ) {
				$status = translate( "You don't have enough gold for this type of trade offer." );
			}
			else {
				$cost = 10;
				$time = 48;
			}
		}
		else {
			if( $has_gold < $gold_offer * $multiplier + 20 * $multiplier ) {
				$status = translate( "You don't have enough gold for this type of trade offer." );
			}
			else {
				$cost = 20;
				$time = 96;
			}
		}
		
		if( $type == 0 ) {
			$tmp_faction_id = -1;
			$private_id = -1;	
		}
		else if( $type == 1 ) {
		    $result3 = mysql_query( "SELECT faction_id " .
		    		"FROM faction_member " .
		    		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die( "Query failed : " . mysql_error() );
			$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
			if( isset( $row3[ 'faction_id' ] ) ) {
				$tmp_faction_id = $row3[ 'faction_id' ];
			}
			else {
				$tmp_faction_id = -1;
			}
			$private_id = -1;
		}
		else if( $type == 2 ) {
			if( isset( $_POST[ 'private_name' ] ) ) {
			    $query = "SELECT ruler_id FROM ruler WHERE name = '" . formatInput( $_POST[ 'private_name' ] ) . "'";
			    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$row = mysql_fetch_array( $result, MYSQL_ASSOC );
				if( isset( $row[ 'ruler_id' ] ) ) {
					if( $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
						$status = translate( "You can't make a private trade offer with yourself." );
					}
					else {
						$tmp_faction_id = -1;
						$private_id = $row[ 'ruler_id' ];
					}
				}
				else {
					$status = $_POST[ 'private_name' ] . " " . translate( "doesn't exist!" );
				}
			}
			else {
				$status = translate( "You must fill in a ruler name." );
			}
		}
	}
	
	if( isset( $status ) ) {
		$_SESSION[ 'gold_offer' ] = $gold_offer;
		$_SESSION[ 'iron_offer' ] = $iron_offer;
		$_SESSION[ 'coal_offer' ] = $coal_offer;
		$_SESSION[ 'food_offer' ] = $food_offer;
		$_SESSION[ 'gold_request' ] = $gold_request;
		$_SESSION[ 'iron_request' ] = $iron_request;
		$_SESSION[ 'coal_request' ] = $coal_request;
		$_SESSION[ 'food_request' ] = $food_request;
		$_SESSION[ 'multiplier' ] = $multiplier;
		
		$_SESSION[ 'value_offered' ] = $value_offered;
		$_SESSION[ 'value_requested' ] = $value_requested;

		$_SESSION[ 'create_trade_status' ] = $status;
		header( "Location: create_trade.php" );
	}
	else {
		$ruler_id_tmp = $_SESSION[ 'ruler_id' ];
		$from_faction = -1;
		$accepted = 0;
		$completed = 0;
		$expired = 0;
		if( $from == 1 ) {
			$from_faction = $tmp_faction_id2;
			$ruler_id_tmp = -1;
		}
		
		for( $i = 0; $i < $multiplier; $i ++ ) {
			$query = "INSERT INTO trade ( gold_offer, iron_offer, coal_offer, food_offer, gold_want, iron_want, coal_want, food_want, private_id, faction_id, ruler_id, turn, time, date, accepted, completed, expired, from_faction ) " . 
						 " VALUES ( $gold_offer, $iron_offer, $coal_offer, $food_offer, $gold_request, $iron_request, $coal_request, $food_request, $private_id, $tmp_faction_id, $ruler_id_tmp, $turn, $time, '$date', $accepted, $completed, $expired, $from_faction )";
			mysql_query( $query ) or die ( "Query failed: " . mysql_error() );
		}

		if( $from == 0 ) {
			mysql_query( "UPDATE ruler " .
					"SET gold = " . ( $has_gold - ( $gold_offer * $multiplier + $cost * $multiplier ) ) . ", " .
							"iron = " . ( $has_iron - $iron_offer * $multiplier ) . ", " .
							"coal = " . ( $has_coal - $coal_offer * $multiplier ) . ", " .
							"food = " . ( $has_food - $food_offer * $multiplier ) . " " .
					"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
		}
		else {
			mysql_query( "UPDATE faction " .
					"SET gold = " . ( $has_gold - ( $gold_offer * $multiplier + $cost * $multiplier ) ) . ", " .
							"iron = " . ( $has_iron - $iron_offer * $multiplier ) . ", " .
							"coal = " . ( $has_coal - $coal_offer * $multiplier ) . ", " .
							"food = " . ( $has_food - $food_offer * $multiplier ) . " " .
						"WHERE leader = " . $_SESSION[ 'ruler_id' ] ) or die ( "Query failed: " . mysql_error() );
		}

		$_SESSION[ 'add_trade_status' ] = translate( "Trade offer succesfully added." );
		header( "Location: trade.php" );
	}
}
?>
